import Vue from "vue";
import { Toast } from "vant";
import VueRouter from "vue-router";
import App from "@/App.vue";

import Login from "@p/login/index.vue";
import Register from "@p/register/index.vue";
import Agreement from "@p/agreement/index.vue";
import AgreementRegister from '@p/agreement/register.vue'

import PayMethod from "@p/pay/payMethod.vue";
import PayInfo from "@p/pay/payInfo.vue";

import RepReim from '@p/errands/index.vue'
import RepReimHome from '@p/errands/wallet/home/index.vue'
import Details from '@p/errands/order/details.vue'
import Balance from '@p/errands/wallet/balance.vue'
import MyBalance from '@p/errands/wallet/mybalance.vue'
import addAccount from '@p/errands/wallet/add.vue'
import Withaccount from '@p/errands/wallet/withaccount.vue'
import editAccount from '@p/errands/wallet/edit.vue'
import ReplaceSupplement from '@p/errands/order/supplement.vue'
import ReplaceFilin from '@p/errands/order/filin.vue'
import RepBackOrder from '@p/errands/order/back.vue'

// 代报员个人中心
import Person from "@p/errands/person/person.vue";
import EditUserInfo from "@p/errands/person/components/edituserinfo.vue";
import EditUserPassword from "@p/errands/person/components/edituserpassword.vue";
import EditUserPhone from "@p/errands/person/components/edituserphone.vue";
import ForgetPassword from "@p/errands/person/components/forgetPassword.vue";
import authentication from "@p/errands/person/components/authentication.vue";
import fileAudit from "@p/errands/order/fileAudit.vue";
import fileAddPage from "@p/errands/order/fileAddPage.vue";

import User from "@p/user/index.vue";
import UserHome from "@p/user/home/index.vue";
import UserHomeDetails from "@p/user/home/details/_id.vue";
import UserHomeSupplement from "@p/user/home/supplement.vue";
import UserApply from "@p/user/apply.vue";
import UserSet from "@p/user/set/index.vue";
import UserAutoApply from "@p/user/autoapply.vue";
import uploadPatent from "@p/user/home/components/uploadPatent.vue";
import orderEditInfo from "@p/user/home/components/orderEditInfo.vue";

import UserEditInfo from "@p/user/set/editInfo.vue";
import UserEditPassWord from "@p/user/set/editPassWord.vue";
import UserEditPhone from "@p/user/set/editPhone.vue";
import UserEditOA from "@p/user/set/setOAAccount.vue";
import UserSetPatent from "@p/user/set/setPatentInfo.vue";

// 代报员注册
import ErrandRegister from '@p/register/errandregister.vue'
Vue.use(VueRouter);

const routes = [
  {
    path: "",
    name: "App",
    component: App,
    redirect: "/login",
    meta: {
      title: "我爱报销"
    }
  },
  {
    path: "/login", // 首页
    name: "Login",
    component: Login,
    meta: {
      title: "用户登录"
    }
  },
  {
    path: "/agreement", // 首页
    name: "Agreement",
    component: Agreement,
    meta: {
      title: "用户协议"
    }
  },
  {
    path: "/agreement/register", // 首页
    name: "AgreementRegister",
    component: AgreementRegister,
    meta: {
      title: "注册协议"
    }
  },
  {
    path: "/user", // 用户首页
    name: "User",
    component: User,
    redirect: "/user/home",
    children: [
      {
        path: "/user/home", //
        name: "UserHome",
        component: UserHome,
        meta: {
          title: "首页",
          requiresAuth: true
        }
      },
      {
        path: "/user/home/details/:id", //
        name: "UserHomeDetails ",
        component: UserHomeDetails,
        meta: {
          title: "详情",
          requiresAuth: false
        }
      },
      {
        path: "/user/home/supplement", //
        name: "UserHomeSupplement ",
        component: UserHomeSupplement,
        meta: {
          title: "补充订单",
          requiresAuth: true
        }
      },
      {
        path: "/user/apply", //
        name: "UserApply",
        component: UserApply,
        meta: {
          title: "订单提交",
          requiresAuth: false
        }
      },
      {
        path: "/user/autoapply", //
        name: "UserAutoApply",
        component: UserAutoApply,
        meta: {
          title: "一键提交",
          requiresAuth: true
        }
      },
      {
        path: "/user/set", //
        name: "UserSet",
        component: UserSet,
        meta: {
          title: "个人中心",
          requiresAuth: true
        }
      },
      {
        path: "/user/set/editInfo", //
        name: "UserEditInfo",
        component: UserEditInfo,
        meta: {
          title: "修改资料",
          requiresAuth: true
        }
      },
      {
        path: "/user/set/editPassWord", //
        name: "UserEditPassWord",
        component: UserEditPassWord,
        meta: {
          title: "修改密码",
          requiresAuth: true
        }
      },
      {
        path: "/user/set/setPatentInfo", //
        name: "UserSetPatent",
        component: UserSetPatent,
        meta: {
          title: "绑定专利申请系统",
          requiresAuth: true
        }
      },
      {
        path: "/user/set/editPhone", //
        name: "UserEditPhone",
        component: UserEditPhone,
        meta: {
          title: "修改手机号",
          requiresAuth: true
        }
      },
      {
        path: "/user/set/setOAAccount", //
        name: "UserEditOA",
        component: UserEditOA,
        meta: {
          title: "修改OA账号",
          requiresAuth: true
        }
      },
      {
        path: "/user/uploadPatent", //
        name: "uploadPatent",
        component: uploadPatent,
        meta: {
          title: "专利订单上传",
          requiresAuth: true
        }
      },
      {
        path: "/user/home/orderEditInfo/:id", //
        name: "orderEditInfo",
        component: orderEditInfo,
        meta: {
          title: "订单资料修改",
          requiresAuth: true
        }
      },
      
      
    ]
  },
  {
    path: "/pay/payMethod",
    name: "PayMethod",
    component: PayMethod,
    meta: {
      title: "支付方式"
    }
  },
  {
    path: "/pay/payInfo",
    name: "PayInfo",
    component: PayInfo,
    meta: {
      title: "确认支付",
      requiresAuth: true
    }
  },
  {
    path: "/register",
    name: "Register",
    component: Register,
    meta: {
      title: "用户注册"
    }
  },
  {
    path: "/errandregister",
    name: "ErrandRegister",
    component: ErrandRegister,
    meta: {
      title: "代报员注册"
    }
  },
  {
    path: "/person",
    name: "person",
    component: Person,
    meta: {
      title: "个人中心",
      requiresAuth: true
    }
  },
  {
    path: "/edituserinfo",
    name: "EditUserInfo",
    component: EditUserInfo,
    meta: {
      title: "修改资料",
      requiresAuth: true
    }
  },
  {
    path: "/authentication",
    name: "authentication",
    component: authentication,
    meta: {
      title: "实名认证",
      requiresAuth: true
    }
  },
  {
    path: "/edituserpassword",
    name: "EditUserPassword",
    component: EditUserPassword,
    meta: {
      title: "修改密码",
      requiresAuth: true
    }
  },
  {
    path: "/edituserphone",
    name: "EditUserPhone",
    component: EditUserPhone,
    meta: {
      title: "修改手机号",
      requiresAuth: true
    }
  },
  {
    path: "/forgetPwd",
    name: "ForgetPassword",
    component: ForgetPassword,
    meta: {
      title: "忘记密码",
    }
  },
  {
    path: "/replace",
    name: "Replace",
    redirect: "/replace/home",
    component: RepReim,
    meta: {
      title: "代报员"
    },
    children:[
        {
            path:'/replace/home',
            name:'ReplaceHome',
            component:RepReimHome,
            meta:{
                title:"首页",
                noKeepAlive: true
            }
        },
        {
            path: '/replace/details',
            component: Details,
            name: 'Details',
            meta: {
                title: '订单信息'
            }
        },
        {
          path: "/replace/supplement",
          name: "ReplaceSupplement ",
          component: ReplaceSupplement,
          meta: {
            title: "上传签字文件"
          },
        },
        {
          path: "/replace/filin",
          name: "ReplaceFilin ",
          component: ReplaceFilin,
          meta: {
            title: "填写报销凭证号"
          },
        },
        {
          path: "/replace/backorder",
          name: "RepBackOrder ",
          component: RepBackOrder,
          meta: {
            title: "退回订单"
          },
        },
        {
            path: '/replace/balance',
            component: Balance,
            name: 'Balance',
            meta: {
                title: '提现'
            }
        },
        {
          path: '/replace/mybalance',
          component: MyBalance,
          name: 'MyBalance',
          meta: {
              title: '我的余额'
          }
        },
        {
            path: '/replace/addAccount',
            component: addAccount,
            name: 'addAccount',
            meta: {
                title: '添加账户'
            }
        },
        {
            path: '/replace/editAccount',
            component: editAccount,
            name: 'editAccount',
            meta: {
                title: '修改账户'
            }
        },
        {
            path: '/replace/withaccount',
            component: Withaccount,
            name: 'withaccount',
            meta: {
                title: '提现账户'
            }
        },
        {
          path: '/replace/fileAudit',
          component: fileAudit,
          name: 'fileAudit',
          meta: {
              title: '文件审核'
          }
        },
        {
          path: '/replace/fileAddPage',
          component: fileAddPage,
          name: 'fileAddPage',
          meta: {
              title: '文件添加'
          }
        },
    ]
},

];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面 title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!sessionStorage || !sessionStorage.getItem("token")) {
      Toast.fail("您还没有登录，请先登录");
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }

  next();
});

export default router;
